package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.RegisterResultInfo;
import com.lanlian.commonlib.bean.ReqRegisterResult;
import com.lanlian.commonlib.bean.RespRegisterResult;
import com.lanlian.commonlib.network.INetWorkCallback;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/6 14:25
 *     desc   : 开户进度查询
 *     version: 1.0
 * </pre>
 */
public class RegisterStatusActivity extends BaseActivity implements View.OnClickListener {

    private static final int MESSAGE_OPEN_NOT_EXIST = 1;//为查询到客户申请信息
    private static final int MESSAGE_OPEN_CHECK = 2;//审核中提示信息
    private static final int MESSAGE_OPEN_NOT_COMMIT_RISJ = 3;//未完成评估
    private static final int MESSAGE_OPEN_SUCCESS = 4;//注册成功
    private static final int MESSAGE_OPEN_FAIL = 5;//审核失败

    private ImageView mImg;
    private TextView mTvContent;
    private TextView mTvOperate;
    private String mMobile;

    private RegisterResultInfo mRegisterResultInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_status, true);
        initData();
        initView();
        setListeners();
        queryStatus();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("开户进度查询结果");
        mImg = findViewById(R.id.img);
        mTvContent = findViewById(R.id.tv_content);
        mTvOperate = findViewById(R.id.tv_operate);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvOperate.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mMobile = getIntent().getStringExtra("mobile");
    }

    private void queryStatus() {
        showLoadingDialog("查询中...");
        VolleyRequestUtils.getInstance().getRegisterResult(this.toString(), new ReqRegisterResult(mMobile),
                new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        dismissLoadingDialog();
                        RespRegisterResult respRegisterResult = (RespRegisterResult) response;
                        mRegisterResultInfo = respRegisterResult.getData();
                        if (mRegisterResultInfo != null) {
                            setRegisterResult();
                        }
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {
                        dismissLoadingDialog();
                        showToast(baseResponse.getMsg());
                        finish();
                    }
                });
    }

    private void setRegisterResult() {
        if (!TextUtils.isEmpty(mRegisterResultInfo.getMsg())) {
            mTvContent.setText(Html.fromHtml(mRegisterResultInfo.getMsg()));
        }
        switch (mRegisterResultInfo.getCode()) {
            case MESSAGE_OPEN_CHECK:
                mImg.setImageResource(R.mipmap.tips_img_audit);
                mTvOperate.setText("确认");
                break;
            case MESSAGE_OPEN_NOT_EXIST:
                mImg.setImageResource(R.mipmap.tips_img_notquerying);
                mTvOperate.setText("去开户");
                break;
            case MESSAGE_OPEN_NOT_COMMIT_RISJ:
                mImg.setImageResource(R.mipmap.tips_img_riskassessment);
                mTvOperate.setText("前往调研");
                break;
            case MESSAGE_OPEN_SUCCESS:
                mImg.setImageResource(R.mipmap.tips_img_adopt);
                mTvOperate.setText("确认");
                break;
            case MESSAGE_OPEN_FAIL:
                mImg.setImageResource(R.mipmap.tips_img_notthrough);
                mTvOperate.setText("确认");
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_operate:
                //审核中：确认关闭页面  没有信息：去开户  还未进行风险调研：调研
                //审核通过：确认关闭页面  审核不通过：确认关闭
                switch (mRegisterResultInfo.getCode()) {
                    case MESSAGE_OPEN_CHECK:
                    case MESSAGE_OPEN_SUCCESS:
                    case MESSAGE_OPEN_FAIL:
                        finish();
                        break;
                    case MESSAGE_OPEN_NOT_EXIST:
                        startActivity(new Intent(this, RegisterMobileActivity.class));
                        finish();
                        break;
                    case MESSAGE_OPEN_NOT_COMMIT_RISJ:
                        startActivity(new Intent(this, RiskStateActivity.class)
                                .putExtra("mobile", mMobile));
                        finish();
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }
}
